﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.CommonSenseControls;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;

namespace SenseTimeTracker.Holidays
{
	public partial class HolidaysEditForm : CommonSenseControls.CommonSenseForm
	{
		private Calendar.Holiday holiday;

		private SensePanelItem holidayItem;
		private SensePanelTextboxItem holidayNameItem;
		private SensePanelComboItem holidayTypeItem;
		private SensePanelDateTimeItemEx holidayDateItem;
		private SensePanelComboItem holidayWeekdayItem;
		private SensePanelNumericItem holidayCountItem;
		private SensePanelNumericItem holidayUtilsationtItem;

		public HolidaysEditForm()
		{
			InitializeComponent();

			holidayItem = new SensePanelItem();
			holidayItem.PrimaryText = string.Empty;
			holidayItem.SecondaryText = string.Empty;
			holidayItem.ShowSeparator = true;

			holidayNameItem = new SensePanelTextboxItem();
			holidayNameItem.LabelText = string.Format("{0}:", Resources.NameLabel);
			holidayNameItem.Text = string.Empty;
			holidayNameItem.MaxLength = DatabaseManager.MaxHolidayNameLength;
			holidayNameItem.ShowSeparator = true;
			holidayNameItem.TextChanged += holidayNameItem_TextChanged;

			holidayTypeItem = new SensePanelComboItem();
			holidayTypeItem.LabelText = string.Format("{0}:", Resources.TypeLabel);
			holidayTypeItem.ShowSeparator = true;
			holidayTypeItem.OnSelectedValueChanged += holidayTypeItem_OnSelectedValueChanged;

			foreach (KeyValuePair<object, string> dateRelation in EnumExtension.GetValues(typeof(Calendar.Holiday.HolidayType)))
			{
				holidayTypeItem.Items.Add(new SensePanelComboItem.Item(dateRelation.Value, dateRelation.Key));
			}

			holidayDateItem = new SensePanelDateTimeItemEx();
			holidayDateItem.LabelText = string.Format("{0}:", Resources.DateLabel);
			holidayDateItem.DisplayStyle = SensePanelDateTimeItem.Style.Date;
			holidayDateItem.ShowSeparator = true;
			holidayDateItem.OnDateTimeChanged += holidayDateItem_OnDateTimeChanged;

			holidayWeekdayItem = new SensePanelComboItem();
			holidayWeekdayItem.LabelText = string.Format("{0}:", Resources.WeekdayLabel);
			holidayWeekdayItem.ShowSeparator = true;
			holidayWeekdayItem.Enabled = false;
			holidayWeekdayItem.OnSelectedValueChanged += holidayWeekdayItem_OnSelectedValueChanged;
			foreach (FieldInfo fieldInfo in typeof(DayOfWeek).GetFields(BindingFlags.Static | BindingFlags.Public))
			{
				string weekdayString = DateTimeFormatInfo.CurrentInfo.GetDayName((DayOfWeek)fieldInfo.GetValue(null));
				holidayWeekdayItem.Items.Add(new SensePanelComboItem.Item(weekdayString, (DayOfWeek)fieldInfo.GetValue(null)));
			}

			holidayCountItem = new SensePanelNumericItem();
			holidayCountItem.PrimaryText = string.Format("{0}:", Resources.RepeatCountLabel);
			holidayCountItem.ShowSeparator = true;
			holidayCountItem.Enabled = false;
			holidayCountItem.OnValueChanged += holidayCountItem_OnValueChanged;

			holidayUtilsationtItem = new SensePanelNumericItem();
			holidayUtilsationtItem.PrimaryText = string.Format("{0}:", Resources.UtilisationLabel);
			holidayUtilsationtItem.ShowSeparator = true;
			holidayUtilsationtItem.Maximum = 100;
			holidayUtilsationtItem.Minimum = 0;

			senseListCtrl.AddItem(holidayItem);
			senseListCtrl.AddItem(holidayNameItem);
			senseListCtrl.AddItem(holidayTypeItem);
			senseListCtrl.AddItem(holidayDateItem);
			senseListCtrl.AddItem(holidayWeekdayItem);
			senseListCtrl.AddItem(holidayCountItem);
			senseListCtrl.AddItem(holidayUtilsationtItem);

			senseHeaderCtrl.Text = Resources.HolidayHeader;

			SIP.Enable(senseListCtrl.Handle);
		}

		void holidayDateItem_OnDateTimeChanged(object Sender, DateTime DateTime)
		{
			holiday.Month = DateTime.Month;
			holiday.Day = DateTime.Day;
			holidayItem.SecondaryText = holiday.Date(DateTime.Year).ToShortDateString();
		}

		void holidayCountItem_OnValueChanged(object Sender, int Value)
		{
			holiday.Count = Value;
			holidayItem.SecondaryText = holiday.Date(0).ToShortDateString();
		}

		void holidayWeekdayItem_OnSelectedValueChanged(object Sender, object Value)
		{
			holiday.DayOfWeek = (DayOfWeek)Value;
			holidayItem.SecondaryText = holiday.Date(0).ToShortDateString();
		}

		void holidayTypeItem_OnSelectedValueChanged(object Sender, object Value)
		{
			holiday.Type = (Calendar.Holiday.HolidayType)Value;
			switch ((Calendar.Holiday.HolidayType)Value)
			{
				case Calendar.Holiday.HolidayType.FixedDate:
					// holidayDateItem.Enabled = true;
					holidayWeekdayItem.Enabled = false;
					holidayCountItem.Enabled = false;
					break;
				case Calendar.Holiday.HolidayType.EasterRelated:
					// holidayDateItem.Enabled = false;
					holidayWeekdayItem.Enabled = true;
					holidayCountItem.Enabled = true;
					holidayDateItem.DateTime = Calendar.Easter(0);
					break;
				case Calendar.Holiday.HolidayType.DateRelated:
					// holidayDateItem.Enabled = true;
					holidayWeekdayItem.Enabled = true;
					holidayCountItem.Enabled = true;
					break;
			}

			holidayItem.SecondaryText = holiday.Date(0).ToShortDateString();
		}

		void holidayNameItem_TextChanged(object sender, EventArgs e)
		{
			holidayItem.PrimaryText = holidayNameItem.Text;
		}

		public Calendar.Holiday Holiday
		{
			get { return holiday; }
			set { holiday = value; }
		}

		protected override void OnLoad(EventArgs e)
		{
			Bind();
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{

			}
			base.OnClosing(e);
		}

		private void Bind()
		{
			holidayItem.PrimaryText = holiday.Name;
			holidayItem.SecondaryText = holiday.Date(0).ToShortDateString();
			holidayNameItem.Text = holiday.Name;
			holidayTypeItem.SelectedValue = holiday.Type;
			holidayDateItem.DateTime = holiday.Date(0);
			holidayWeekdayItem.SelectedValue = holiday.DayOfWeek;
			holidayUtilsationtItem.Value = (int)holiday.Utilisation;
		}
	}
}

